import React from 'react'
import { Button, Card, Form, Input, Checkbox, message } from "antd"
import Styles from './index.module.scss'
import logo from '../../assets/logo.png'
import { loginAPI } from '../../api/user'
import { setToken } from '../../utils/token'
export default function Index({ history }) {
  const submitFn = (values) => {
    const request = async () => {
      // console.log(values);
      try {
        const res = await loginAPI(values)
        // console.log(res);
        // localStorage.setItem('geek-pc', res.data.token)'
        setToken(res.data.token)
        // console.log(this.props);
        history.push('/layout')
        message.success('登录成功', 1.5)
      } catch (error) {
        // console.dir(error.response.data.message);
        message.error(error.response.data.message, 1.5)
      }
    }
    request()
  }
  return (
    <div className={Styles.login}>
      <Card className="login-container">
        <img className="login-logo" src={logo} alt="" />
        <Form
          onFinish={submitFn}
          initialValues={{
            code: '246810',
            mobile: '13911111111',
            remember: true
          }}
          autoComplete="off"
        >
          <Form.Item
            name="mobile"
            rules={[
              { required: true, message: '手机号码不能为空!' },
              { pattern: /^1[3-9][0-9]{9}$/, message: '请输入正确手机号码' }
            ]}
          >
            <Input placeholder='请输入手机号码' />
          </Form.Item>

          <Form.Item
            name="code"
            rules={[
              { required: true, message: '验证码不能为空!' },
              { pattern: /^\d{6}$/, message: '验证码格式错误' }
            ]}
          >
            <Input.Password placeholder='请输入验证码' />
          </Form.Item>

          <Form.Item name="remember" valuePropName="checked" rules={[
            { validator: (_, value) => value ? Promise.resolve() : Promise.reject(new Error('请阅读协议并同意')) }
          ]}>
            <Checkbox>我已阅读并同意[用户协议]和[隐私条款]</Checkbox>
          </Form.Item>

          <Form.Item >
            <Button block type="primary" htmlType="submit">
              登录
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  )
}


